home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / ipmap.c < prev    next >
C/C++ Source or Header  |  1996-06-30  |  5KB  |  215 lines

  1. /* #Specification: dnsconf / ip range / strategy
  2.     linuxconf allows one to qualify the different IP numbers of his
  3.     network. For exemple, one managing a class C network such as
  4.     192.168.1.0 may want to organised things this way
  5.  
  6.     #
  7.     192.168.1.1-20        Servers
  8.     192.168.1.21-40        IP for PPP dialin
  9.     192.168.1.41-60        Virtual domain
  10.     192.168.1.61-254    Reserved
  11.     #
  12.  
  13.     This has two effects. First it documents the network and allows a
  14.     new operator to have some clues. Next, it allows linuxconf to help
  15.     you find an unused IP number.
  16.  
  17.     The user interface for managing IP number use this and will locate
  18.     one unused IP number in every range (If possible).
  19. */
  20. #include <string.h>
  21. #include "../misc/misc.h"
  22. #include "dnsconf.h"
  23. #include "internal.h"
  24. #include "dnsconf.m"
  25.  
  26. static DNSCONF_HELP_FILE help_iprange("iprange");
  27.  
  28. PUBLIC IPMAP::IPMAP(const char *line)
  29. {
  30.     line = iprange.copyword (line);
  31.     line = str_skip(line);
  32.     comment.setfrom (line);
  33. }
  34. PUBLIC IPMAP::IPMAP()
  35. {
  36. }
  37.  
  38. /*
  39.     Parse the IP range and prepare for the search of an available IP.
  40.     Return -1 if the IP range is invalid (not x.y.z.w1-w2)
  41. */
  42. PUBLIC int IPMAP::setup()
  43. {
  44.     int ret = -1;
  45.     char tmp[200];
  46.     iprange.copy(tmp);
  47.     char *pt = strchr(tmp,'-');
  48.     over = 0;
  49.     if (pt != NULL){
  50.         *pt++ = '\0';
  51.         minimum.setfrom(tmp);
  52.         if (minimum.is_valid()){
  53.             IP_ADDR part;
  54.             part.setfrom(str_skip(pt));
  55.             avail.setfrom (tmp);
  56.             maximum.setfrom (tmp);
  57.             part.shift_right();
  58.             maximum.merge (part);
  59.             if (maximum.is_valid()) ret = 0;
  60.         }
  61.     }
  62.     return ret;
  63. }
  64.  
  65. /*
  66.     Record the fact that the IP "adr" is in use.
  67.     This function assume that it will be called with adr always increasing.
  68. */
  69. PUBLIC void IPMAP::setuse (const IP_ADDR *adr)
  70. {
  71.     if (minimum.cmp(adr)<=0
  72.         && maximum.cmp(adr)>=0){
  73.         if (avail.cmp(adr)==0){
  74.             if (avail.cmp(&maximum)==0){
  75.                 over = 1;
  76.             }else{
  77.                 avail.increm();
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83. static char IPDB[]="IPDB";
  84. static char RANGE[]="RANGE";
  85.  
  86. PUBLIC IPMAP *IPMAPS::getitem(int no)
  87. {
  88.     return (IPMAP*)ARRAY::getitem(no);
  89. }
  90.  
  91. PUBLIC IPMAPS::IPMAPS()
  92. {
  93.     SSTRINGS lst;
  94.     int nb = linuxconf_getall (IPDB,RANGE,lst,0);
  95.     for (int i=0; i<nb; i++){
  96.         add (new IPMAP(lst.getitem(i)->get()));
  97.     }
  98. }
  99.  
  100. /*
  101.     Update /etc/conf.linuxconf
  102. */
  103. PUBLIC void IPMAPS::save()
  104. {
  105.     linuxconf_removeall (IPDB,RANGE);
  106.     int n = getnb();
  107.     for (int i=0; i<n; i++){
  108.         IPMAP *e = getitem(i);
  109.         if (!e->iprange.is_empty()){
  110.             char buf[200];
  111.             sprintf (buf,"%s %s",e->iprange.get(),e->comment.get());
  112.             linuxconf_add (IPDB,RANGE,buf);
  113.         }
  114.     }
  115.     linuxconf_save();
  116. }
  117.  
  118.  
  119. /*
  120.     Compute the available IP number in each IP address range.
  121.     adrs will be sort by this algoryth.
  122. */
  123. PUBLIC void IPMAPS::setuse (IP_ADDRS &adrs)
  124. {
  125.     int n = getnb();
  126.     int i;
  127.     for (i=0; i<n; i++){
  128.         getitem(i)->setup();
  129.     }
  130.     adrs.sort();
  131.     int na = adrs.getnb();
  132.     for (i=0; i < na; i++){
  133.         IP_ADDR *adr = adrs.getitem(i);
  134.         for (int j=0; j<n; j++){
  135.             getitem(j)->setuse(adr);
  136.         }
  137.     }
  138.     for (i=0; i<n; i++){
  139.         getitem(i)->avail.reformat();
  140.     }
  141. }
  142.  
  143. /*
  144.     Setup the comp field options helping in IP number allocation.
  145. */
  146. PUBLIC void IPMAPS::setcombo (class FIELD_COMBO *comb)
  147. {
  148.     int n = getnb();
  149.     for (int i=0; i<n; i++){
  150.         IPMAP *e = getitem(i);
  151.         const char *str = e->avail.get();
  152.         if (e->over){
  153.             str = MSG_U(F_RANGEFULL,"None available");
  154.         }
  155.         comb->addopt (str,e->comment.get());
  156.     }
  157. }
  158.     
  159. PUBLIC void IPMAPS::edit()
  160. {
  161.     int no = 0;
  162.     if (getnb()==0)    add (new IPMAP);
  163.     while (1){
  164.         DIALOG dia;
  165.         int n = getnb();
  166.         for (int i=0; i<n; i++){
  167.             IPMAP *e = getitem(i);
  168.             dia.newf_str (MSG_U(F_IPRANGE,"One IP range"),e->iprange);
  169.             dia.newf_str (MSG_U(F_RANGECOMMENT,"Comment"),e->comment);
  170.         }
  171.         MENU_STATUS code = dia.edit (MSG_U(T_IPRANGE,"IP range definitions")
  172.             ,MSG_U(I_IPRANGE
  173.                 ,"You can define here the range of IP numbers available\n"
  174.                  "to this DNS. It won't limit your ability of assigning\n"
  175.                  "the IP number you want, but will allow you to find\n"
  176.                  "an available one easily later.")
  177.                 ,help_iprange
  178.                 ,no
  179.                 ,MENUBUT_ADD|MENUBUT_CANCEL|MENUBUT_ACCEPT);
  180.         if (code == MENU_CANCEL || code == MENU_ESCAPE){
  181.             dia.restore();
  182.             break;
  183.         }else if (code == MENU_ACCEPT){
  184.             int err = 0;
  185.             for (int j=0; j<n; j++){
  186.                 IPMAP *e = getitem(j);
  187.                 if (!e->iprange.is_empty()
  188.                     && e->setup()==-1){
  189.                     no = j*2;
  190.                     err = 1;
  191.                     xconf_error (MSG_U(E_IVLDIPMAP
  192.                         ,"Invalid IP range definition %s\n"
  193.                          "Expected X.Y.Z.W1-W2\n"
  194.                          "or       X.Y.Z1.W1-Z2.W2")
  195.                         ,e->iprange.get());
  196.                     break;
  197.                 }
  198.             }
  199.             if (!err){
  200.                 save();
  201.                 break;
  202.             }
  203.         }else{
  204.             add (new IPMAP);
  205.         }
  206.     }
  207. }
  208.  
  209. void ipmap_edit ()
  210. {
  211.     IPMAPS maps;
  212.     maps.edit();
  213. }
  214.  
  215.